package CommentApi.Output;

import CommentApi.Comments.ClassComment;
import CommentApi.Comments.MethodComment;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Data
public class ControllerInfo{
    private ClassComment controllerClass;
    private List<UriInfo> uriInfoList;
    public ControllerInfo(ClassComment classComment, List<MethodComment> list){
        controllerClass = classComment;
        initUriInfoList(classComment, list);
    }
    private void initUriInfoList(ClassComment classComment,List<MethodComment> list){
        String[] rootUris;
        RequestMapping rootUriRequestMapping = classComment.getAClass().getAnnotation(RequestMapping.class);

        if (rootUriRequestMapping == null) {
            rootUris = new String[]{""};
        }else {
            rootUris = rootUriRequestMapping.value();
        }
        this.uriInfoList = list.stream()
                .map(methodComment -> new UriInfo(rootUris, methodComment))
                .collect(Collectors.toList());
    }
}
